<template>
  <el-card shadow="always">
    <el-form inline :disabled="disabled">
      <el-form-item label="一级分类">
        <el-select placeholder="请选择" v-model="categoryStore.category1Id">
          <el-option
            v-for="C1Item in categoryStore.C1List"
            :key="C1Item.id"
            :label="C1Item.name"
            :value="C1Item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="二级分类">
        <el-select placeholder="请选择" v-model="categoryStore.category2Id">
          <el-option
            v-for="C2Item in categoryStore.C2List"
            :key="C2Item.id"
            :label="C2Item.name"
            :value="C2Item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="三级分类">
        <el-select v-model="categoryStore.category3Id" placeholder="请选择">
          <el-option
            v-for="C3Item in categoryStore.C3List"
            :key="C3Item.id"
            :label="C3Item.name"
            :value="C3Item.id"
          />
        </el-select>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script lang="ts">
export default {
  name: "CategorySelector",
};
</script>

<script lang="ts" setup>
import { watch, onActivated, onDeactivated } from "vue";
import { useCategoryStore } from "@/stores/category";
defineProps<{
  disabled: boolean;
}>();

const categoryStore = useCategoryStore();
// 🐷一级分类列表
// 挂载的时候请求一级分类列表
onActivated(() => {
  categoryStore.getCategory1List();
});
// 🐷二级分类列表
// 监视category1Id
watch(
  () => categoryStore.category1Id,
  () => {
    categoryStore.getCategory2List();
  }
);
// 🐷三级分类列表
// 监视category2Id
watch(() => categoryStore.category2Id, categoryStore.getCategory3List);

onDeactivated(() => {
  categoryStore.$reset();
});
</script>

<style scoped>
:deep(.el-form-item) {
  margin-top: 10px;
  margin-bottom: 10px;
}
</style>
